<template>
  <div class="app-container">
    <!-- 头部 -->
    <el-row
      :gutter="15"
      class="mb8"
    >
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-edit"
          :disabled="single"
          size="mini"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-delete"
          :disabled="multiple"
          size="mini"
          @click="handleDelete"
        >删除</el-button>
      </el-col>

      <el-form
        :model="queryParams"
        ref="queryFormRef"
        v-show="showSearch"
        :inline="true"
        class="el-form-search"
      >
        <!-- 类型名称 -->
        <el-form-item
          label="名称"
          prop="name"
          class="el-form-search-item"
        >
          <el-input
            clearable
            placeholder="请输入类型名称"
            v-model="queryParams.name"
            size="mini"
            style="width: 140px"
          ></el-input>
        </el-form-item>
        <!--类型代码-->
        <el-form-item
          label="代码"
          prop="code"
          class="el-form-search-item"
        >
          <el-input
            clearable
            placeholder="请输入类型代码"
            v-model="queryParams.code"
            size="mini"
            style="width: 140px"
          ></el-input>
        </el-form-item>
        <!--所有按钮-->
        <el-form-item class="el-form-search-item">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleSearch"
          >搜索</el-button>
          <el-button
            icon="el-icon-refresh"
            size="mini"
            @click="resetQuery"
          >重置</el-button>
        </el-form-item>
      </el-form>
    </el-row>

    <!-- 数据列表展示 -->
    <el-table
      :height="tableHeight"
      ref="elTable"
      :data="devModelList"
      v-loading="loading"
      highlight-current-row
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        with="105"
        align="center"
        :resizable="false"
      ></el-table-column>
      <el-table-column
        label="#"
        type="index"
        width="100"
        align="center"
        :resizable="false"
      >
        <template slot-scope="scope">
          <span>{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
            }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="名称"
        prop="name"
        width="300"
        align="center"
        :resizable="false"
      >
      </el-table-column>
      <el-table-column
        label="编码"
        prop="code"
        width="300"
        align="center"
        :resizable="false"
      >
      </el-table-column>
      <el-table-column
        label="备注"
        prop="remark"
        width="450"
        align="center"
        :resizable="false"
      >
      </el-table-column>
      <el-table-column
        label="操作"

        align="center"
        :resizable="false"
      >
        <template slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页控件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增或者修改对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="30%"
      v-dialogDrag
      :close-on-click-modal="false"
    >
      <el-form
        :model="formData"
        :rules="rules"
        ref="formDataRef"
        label-width="80px"
        @submit.prevent
      >
        <!--类型名称-->
        <el-form-item
          label="类型名称"
          prop="name"
        >
          <el-input
            clearable
            placeholder="请输入类型名称"
            v-model.trim="formData.name"
          ></el-input>
        </el-form-item>
        <!--类型代码-->
        <el-form-item
          label="类型代码"
          prop="code"
        >
          <el-input
            clearable
            placeholder="请输入类型代码"
            v-model.trim="formData.code"
          ></el-input>
        </el-form-item>
        <!--备注-->
        <el-form-item
          label="备注"
          prop="remark"
        >
          <el-input
            clearable
            placeholder="备注"
            type="textarea"
            :rows="5"
            :maxlength="150"
            resize="none"
            show-word-limit
            v-model.trim="formData.remark"
          ></el-input>
        </el-form-item>
      </el-form>

      <div
        slot="footer"
        class="dia-footer"
      >
        <el-button
          type="primary"
          @click="submitFormData"
        >提交</el-button>
        <el-button @click="close">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDevModel,
  addDevModel,
  getByNum,
  updateDevModel,
  delDevModel,
} from "@/api/device/devModel";

export default {
  data () {
    return {
      // 遮罩层
      loading: true,

      /**搜索框参数 */
      //单选
      single: true,
      //多选
      multiple: true,
      // 是否显示搜索框
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 30,
        name: undefined,
        code: undefined,
        orderByColumn: "update_time",
        isAsc: "asc",
      },

      /**表格参数 */
      // 表格的高度
      tableHeight: "calc(100vh - 200px)",
      // 表格数据
      devModelList: [],
      //选择列表
      ids: [],

      //总行数
      total: 0,

      /**新增/修改设备类型对话框相关 */
      title: "",
      open: false,
      formData: {},
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
      },
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /**查询设备类型列表 */
    getList () {
      this.loading = true;
      listDevModel(this.queryParams).then((response) => {
        this.devModelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 新增 */
    handleAdd () {
      this.resetForm("formDataRef");
      this.formData = {};
      this.open = true;
      this.title = "新增设备类型";
    },
    /**修改 */
    handleUpdate (row) {
      this.resetForm("formDataRef");
      this.open = true;
      this.title = "修改设备类型";
      const nums = row.num || this.ids;
      getByNum(nums).then((response) => {
        this.formData = response.data;
      });
    },
    /**删除 */
    handleDelete (row) {
      const nums = row.num || this.ids;
      this.$modal
        .confirm(`确认删除吗？`)
        .then(function () {
          return delDevModel(nums);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /**搜索方法 */
    handleSearch () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /**重置搜索框 */
    resetQuery () {
      // this.resetForm("queryFormRef");
      this.$refs["queryFormRef"].resetFields();
      this.handleSearch();
    },
    /**表格勾选框变化 */
    handleSelectionChange (val) {
      this.ids = val.map((item) => item.num);
      console.info(this.ids);
      this.single = this.ids.length != 1;
      this.multiple = !this.ids.length;
    },
    /**提交对话框数据 */
    submitFormData () {
      this.$refs["formDataRef"].validate((valid) => {
        if (valid) {
          console.info(this.formData);
          //判断是修改还是新增
          if (this.formData.num != undefined) {
            //修改
            updateDevModel(this.formData).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            //新增
            addDevModel(this.formData).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /**取消对话框 */
    close () {
      this.resetForm("formDataRef");
      this.open = false;
    },
  },
};
</script>
